package com.kaibes.core.base.util;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

import com.kaibes.core.base.BaseConfig;

public class LocalDateTimeUtils {

    public static LocalDateTime fromMilliseconds(long milliseconds) {
        return LocalDateTime.ofEpochSecond(milliseconds/1000, 0, BaseConfig.zoneOffset);
    }
    
    public static long between(LocalDateTime start, LocalDateTime end) {
        long st =start.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long et = end.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        return et - st;
    }
    
    public static String format(LocalDateTime date, String format) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        return date.format(formatter);
    }
    
    public static String format(LocalDateTime date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        if (date != null) {
            return date.format(formatter);
        } else {
            return "";
        }
    }
    
}
